/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package patternmining.model.graph;

import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import java.util.Collection;
import patternmining.util.CollectionOperations;

/**
 *
 * @author Holger
 */
public class JungImplementation implements GraphImplementation{
    
    private DirectedSparseMultigraph<Vertex, Edge> graph;
    
    public JungImplementation(){
        graph = new DirectedSparseMultigraph<Vertex, Edge>();
    }

    public String toString(){
        return graph.toString();
    }

    public void add(Vertex vertex){
        graph.addVertex(vertex);
    }

    public void add(Edge edge){
        graph.addEdge(edge, edge.getOrigin(), edge.getDestination(), EdgeType.DIRECTED);
    }
    
    public Collection<Vertex> getVertices(){
        return graph.getVertices();
    }
    
    public Collection<Vertex> getSuccessors(Vertex vertex){
        return graph.getSuccessors(vertex);
    }
    
    public Collection<Edge> getOutEdges(Vertex vertex){
        return graph.getOutEdges(vertex);
    }
    public Collection<Edge> getEdges(){
        return graph.getEdges();
    }
    
    public Edge getEdge(Vertex origin, Vertex destination){
        Edge edge = null;
        //important: if the two vertices are connected by multiple parallel edges, any one of these is returned
        //improve method
        edge = graph.findEdge(origin, destination);
        return edge;
    }
    
    public boolean existsParalellEdge(Edge edge){
        boolean parallel = false;
        if( graph.findEdgeSet(edge.getOrigin(), edge.getDestination()).size() > 1){
            parallel = true;
        }
        return parallel;
    }
    public Collection<Edge> getParallelEdges(Edge edge){
        Collection<Edge> parallelEdges =  graph.findEdgeSet(edge.getOrigin(), edge.getDestination());
        CollectionOperations<Edge> operations = new CollectionOperations<Edge>();
        Collection parallels = operations.difference(parallelEdges, edge);
        return parallels;
    }
    
}
